//
//  HHStatusController.m
//  oschina
//
//  Created by hjl on 16/3/31.
//  Copyright © 2016年 hjl. All rights reserved.
//

#import "HHStatusController.h"
#import "HHTweetController.h"
@interface HHStatusController () <UIScrollViewDelegate>
/** contentView */
@property (nonatomic, strong) UIScrollView *contentView;

/** titleView */
@property (nonatomic, strong) UIView *titleView;

/** selectedButton */
@property (nonatomic, strong) UIButton *selectedButton;

@end

@implementation HHStatusController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 设置contentView
    [self setupContentView];
    
    // 设置子控件
    [self setupChildViewControllers];
    
    // 设置标题栏
    [self setupTitleView];
}

// 设置contentView
- (void)setupContentView{
    // 创建UIScrollView
    UIScrollView *contentView = [[UIScrollView alloc]init];
    // 设置frame
    contentView.frame = self.view.bounds;
    // 插入到控制器
    [self.view insertSubview:contentView atIndex:0];
    // 属性赋值
    self.contentView = contentView;
    // 禁止弹簧效果
    contentView.bounces = NO;
    // 设置代理
    self.contentView.delegate = self;
    // 设置分页
    contentView.pagingEnabled = YES;
    // 设置滚动区域
    contentView.contentSize = CGSizeMake(HHWidth * 3, 0);
    // 水平滚动条
    contentView.showsHorizontalScrollIndicator = NO;
}

// 设置contentView
- (void)setupChildViewControllers{
    
    NSArray *nameArr = @[@"最近动弹", @"热门动弹", @"我的动弹"];
    NSArray *typeArr = @[@(0), @(-1), @(1)];
    for (int i = 0; i < nameArr.count; i++) {
        HHTweetController *vc = [[HHTweetController alloc]init];
        vc.type = [typeArr[i] intValue];
        vc.title = nameArr[i];
        vc.view.frame = CGRectMake(HHWidth * i, 0, HHWidth, HHHeight);
        [self.contentView addSubview:vc.view];
        [self addChildViewController:vc];
    }
}

// 设置标题框
- (void)setupTitleView{
    UIView *titleView = [[UIView alloc]init];
    titleView.backgroundColor = [UIColor whiteColor];
    titleView.frame = CGRectMake(0, 64, HHWidth, 44);
    [self.view addSubview:titleView];
    self.titleView = titleView;
    
    // 添加3个按钮
    CGFloat btnY = 0;
    CGFloat btnH = self.titleView.height;
    CGFloat btnW = HHWidth / self.childViewControllers.count;
    
    for (int i = 0; i < self.childViewControllers.count; i++) {
        UIButton *btn = [[UIButton alloc] init];
        btn.frame = CGRectMake(i * btnW, btnY, btnW, btnH);
        
        btn.titleLabel.font = [UIFont systemFontOfSize:16];
        [btn setTitle:self.childViewControllers[i].title forState:UIControlStateNormal];
        [btn setTitleColor:HHRGBColor(120, 120, 120, 1.0) forState:UIControlStateNormal];
        [btn setTitleColor:HHBackgroundColor forState:UIControlStateSelected];
        
        btn.backgroundColor = HHRGBColor(222, 222, 222, 1.0);
        [self.titleView addSubview:btn];
        
        btn.tag = i;
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        if (i == 0) {
            btn.selected = YES;
            self.selectedButton = btn;
        }
    }
}

- (void)btnClick:(UIButton *)button{
    // 取消当前选中按钮的选中状态
    self.selectedButton.selected = NO;
    
    // 设置选中按钮的选中状态
    button.selected = YES;
    
    // 设置点击按钮为当前选中按钮
    self.selectedButton = button;
    
    CGPoint offset =  self.contentView.contentOffset;
    
    offset.x = button.tag * HHWidth;
    
    [self.contentView setContentOffset:offset animated:YES];
    
}
// 滚动结束后,调用按钮的方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    CGPoint offset = scrollView.contentOffset;
    NSInteger index = offset.x / HHWidth;
    [self btnClick:self.titleView.subviews[index]];
}


@end
